//
//  UIView+ SGExtension.swift
//  TravelSwift
//
//  Created by 智蚂优服 on 2018/12/21.
//  Copyright © 2018 智蚂优服. All rights reserved.
//

import Foundation
import UIKit
extension UIView : ZMCompatible{}

//@IBDesignable
extension UIView {
    //圆角
    @IBInspectable var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
            layer.masksToBounds = newValue > 0
        }
    }
    
    @IBInspectable var masksToBounds: Bool {
        get {
            return layer.masksToBounds
        }
        set {
            layer.masksToBounds = newValue
        }
    }
    
    //边框宽度
    @IBInspectable var borderWidth: CGFloat {
        get {
            return layer.borderWidth
        }
        set {
            layer.borderWidth = newValue
        }
    }
    
    //边框颜色
    @IBInspectable var borderColor: UIColor? {
        get {
            if (layer.borderColor != nil) {
                return UIColor(cgColor: layer.borderColor!)
            }
            return self.backgroundColor
        }
        set {
            layer.borderColor = newValue?.cgColor
        }
    }
}


extension ZM where Base: UIView {
    var x: CGFloat {
        get {
            return base.frame.origin.x
        }
    }
    
    func setX(_ v : CGFloat) {
        var fram = base.frame
        fram.origin.x = v
        base.frame = fram
    }

    var y: CGFloat {
        get {
            return base.frame.origin.y
        }
    }
    
    func setY(_ v : CGFloat) {
        var fram = base.frame
        fram.origin.y = v
        base.frame = fram
    }

    var bottom: CGFloat {
        get {
            return base.frame.origin.y + base.frame.size.height
        }
    }
    
    func setBottom(_ v : CGFloat) {
        var fram = base.frame
        fram.origin.y = v - fram.size.height
        base.frame = fram
    }

    var right: CGFloat {
        get {
            return base.frame.origin.x + base.frame.size.width
        }
    }
    
    func setRight(_ v : CGFloat) {
        var fram = base.frame
        fram.origin.x = v - fram.size.width
        base.frame = fram
    }

    var width: CGFloat {
        get {
            return base.frame.size.width
        }
    }
    
    func setWidth(_ v : CGFloat) {
        var fram = base.frame
        fram.size.width = v
        base.frame = fram
    }

    var height: CGFloat {
        get {
            return base.frame.size.height
        }
    }
    
    func setHeight(_ v : CGFloat) {
        var fram = base.frame
        fram.size.height = v
        base.frame = fram
    }
    
    var size: CGSize {
        get {
            return base.frame.size
        }
    }
    
    func setSize(_ v : CGSize) {
        var fram = base.frame
        fram.size = v
        base.frame = fram
    }
    
    var centerX : CGFloat {
        get {
            return base.center.x
        }
    }
    
    func setCenterX(_ v : CGFloat) {
        var p = base.center
        p.x = v
        base.center = p
    }
    
    var centerY : CGFloat {
        get {
            return base.center.y
        }
    }
    
    func setCenterY(_ v : CGFloat) {
        var p = base.center
        p.y = v
        base.center = p
    }

    var tailing : CGFloat {
        get {
            return base.frame.size.width + base.frame.origin.x
        }
    }
    
    func setTailing(_ v : CGFloat) {
        var fram = base.frame
        fram.origin.x = v - fram.size.width
        base.frame = fram
    }
    
    
    ///设置多个圆角UIRectCorner(rawValue: (UIRectCorner.topRight.rawValue) | (UIRectCorner.bottomRight.rawValue))
    public func filletedCorner(_ cornerRadii: CGFloat, _ roundingCorners: UIRectCorner) {
        let fieldPath = UIBezierPath.init(roundedRect: base.bounds, byRoundingCorners: roundingCorners, cornerRadii:CGSize(width: cornerRadii, height: cornerRadii))
        let fieldLayer = CAShapeLayer()
        fieldLayer.frame = base.bounds
        fieldLayer.path = fieldPath.cgPath
        base.layer.mask = fieldLayer
    }
    
    //MARK:- 绘制虚线边框
    public func addDashdeBorderLayer(strokeColor: UIColor, lineWidth: CGFloat = 1, lineLength: Int = 10, lineSpacing: Int = 5) {
        let layer = CAShapeLayer()
        layer.frame = base.bounds
        layer.backgroundColor = UIColor.clear.cgColor
        layer.lineDashPattern = [NSNumber(value: lineLength), NSNumber(value: lineSpacing)]
        let path = UIBezierPath(roundedRect: layer.frame, cornerRadius: 4.0)
        layer.path = path.cgPath
        layer.lineWidth = lineWidth
        layer.fillColor = UIColor.clear.cgColor
        layer.strokeColor = strokeColor.cgColor
        base.layer.addSublayer(layer)
    }
    
    //MARK:- 阴影圆角
    public func setShadowFillet(shadowColor: UIColor, cornerRadius: CGFloat, shadowOffset: CGSize) {
//        base.superview.subviews.forEach { (view) in
//            if view.tag == 99999999 {
//                view.removeFromSuperview()
//            }
//        }
        let bgView = UIView(frame: base.frame)
//        bgView.tag = 99999999
        bgView.layer.shadowColor = shadowColor.cgColor
        bgView.layer.shadowOffset = shadowOffset
        bgView.layer.shadowOpacity = Float(shadowColor.cgColor.alpha)
        bgView.layer.shadowRadius = cornerRadius
        bgView.layer.cornerRadius = cornerRadius
        let path = UIBezierPath.init(roundedRect: bgView.bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize.init(width: cornerRadius, height: cornerRadius))
        bgView.layer.shadowPath = path.cgPath
        base.superview?.insertSubview(bgView, at: 0)
    }
    
    //MARK:- 渐变色背景
    func addlayer(_ colors: [CGColor] = [Style.mainColor.cgColor, Style.mainColor.cgColor], _ locations: [NSNumber] = [0, 1], _ startPoint: CGPoint = CGPoint(x: 0.24, y: 0.19), _ endPoint: CGPoint = CGPoint(x: 0.84, y: 0.85)) {
        let bglayer = CAGradientLayer()
        bglayer.colors = colors
        bglayer.locations = locations
        bglayer.frame = base.bounds
        bglayer.startPoint = startPoint
        bglayer.endPoint = endPoint
        base.layer.insertSublayer(bglayer, at: 0)
    }
    
    /// 删除线
    func addDeleteLine(_ color: UIColor) {
        let attributeStr = NSMutableAttributedString(string: (base as! UILabel).text!)
        attributeStr.addAttributes([.strikethroughStyle: NSNumber(value: 1), .underlineColor: color], range: NSMakeRange(0, attributeStr.length))
        (base as! UILabel).attributedText = attributeStr
    }
    
    /// 找响应链中的父控制器
    func getFaterVC() -> UIViewController? {
        var theV : UIView = base
        var vc : UIViewController?
        while theV.superview != nil {
            theV = theV.superview!
            if let nR = theV.next, nR is UIViewController{
               vc = nR as? UIViewController
               break
            }
        }
        return vc
    }
    
    // 添加tap手势
    func addTapGestureRecognizer(target: Any?, action: Selector?) -> UITapGestureRecognizer {
        let tap = UITapGestureRecognizer.init(target: target, action: action)
        base.isUserInteractionEnabled = true
        base.addGestureRecognizer(tap)
        return tap
    }
    
    func addTap(target : Any, selector : Selector) {
        base.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: target, action: selector)
        base.addGestureRecognizer(tap)
    }
}
